package org.gwtEss.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.gwtEss.client.GreetingService;
import org.gwtEss.client.GwtEss;
import org.gwtEss.client.SimpleVO;
import org.gwtEss.client.TableInfoVO;
import org.gwtEss.table.FilterMenuItem;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	public static List<SimpleVO> data;
	public static int MAX_RECS = 500;
	@Override
	protected void onBeforeRequestDeserialized(String serializedRequest) {
		if (data == null){
			data = new ArrayList<SimpleVO>();
			for (int i=0; i<MAX_RECS; i++){
				data.add(new SimpleVO(i, null, null, null, null, i*2, null, "test data " + i));
			}
		}
		super.onBeforeRequestDeserialized(serializedRequest);
	}
	public String greetServer(String input) {
		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}
	
	public List<SimpleVO> fetchData(ArrayList<Integer> rowNumList){
		ArrayList<SimpleVO> retLst = new ArrayList<SimpleVO>();
		for (int i=0; i<rowNumList.size(); i++){
			int rowNum = rowNumList.get(i);
			retLst.add(new SimpleVO(rowNum, null, null, null, null, rowNum*2, null, "test data " + rowNum));
		}
		return retLst;
	}

	private void sortData(String sortColName, final Boolean sortColAsc){
		if (sortColName.equals(colNames[0]) || sortColName.equals(colNames[2])  || sortColName.equals(colNames[4]) )
			Collections.sort(data, new Comparator<SimpleVO>(){

				public int compare(SimpleVO o1, SimpleVO o2) {
					if (sortColAsc)
						return o1.getStrCol().compareTo(o2.getStrCol());
					else 
						return o2.getStrCol().compareTo(o1.getStrCol());
				}
				
			});
		
		else if (sortColName.equals(colNames[1]))
			Collections.sort(data, new Comparator<SimpleVO>(){

				public int compare(SimpleVO o1, SimpleVO o2) {
					if (sortColAsc)
						return o1.getStrCol().compareTo(o2.getStrCol());
					else 
						return o2.getStrCol().compareTo(o1.getStrCol());
				}
				
			});
		else if (sortColName.equals(colNames[3]))
			Collections.sort(data, new Comparator<SimpleVO>(){

				public int compare(SimpleVO o1, SimpleVO o2) {
					if (sortColAsc)
						return o1.getDateCol().compareTo(o2.getDateCol());
					else 
						return o2.getDateCol().compareTo(o1.getDateCol());
				}
				
			});
	}
	public List<SimpleVO> fetchData(int startRow, int endRow, String sortColName,
			Boolean sortColAsc) {
		
		if (sortColName != null)
			sortData(sortColName, sortColAsc);
		ArrayList<SimpleVO> ret = new ArrayList<SimpleVO>();;;
		for (int i=startRow; i< endRow; i++)
			ret.add(data.get(i));
		return ret;
	}
/*	private Map<String, List<String>> getFilters(){
		for (String colNames: colNames){
			if ()
		}
	}
*/
	
	public static final String[] colNames = {"read only column", "user defined column", "Smart user managed", "user defined column", "user managed text box col"};
	public List<FilterMenuItem> fetchFilterList(String colName) {
		List<FilterMenuItem> ret = null;
		if (colName.equals(colNames[0])){
			ret = new ArrayList<FilterMenuItem>();
			FilterMenuItem item = new FilterMenuItem();
			for (SimpleVO row: data){
				item = new FilterMenuItem(row.getStrCol(), false);
				if (ret.indexOf(item) == -1);
				ret.add(item);
			}
		}
		return ret;
	}
	public List<SimpleVO> fetchData(int startRow, int endRow,
			String sortColName, Boolean sortColAsc,
			HashMap<String, ArrayList<String>> filters) {
		return null;
	}
}
